import * as React from "react";
import { Button, Input } from "antd";
import moment from "moment";

import EleAndWater from "../../../../../../compontents/EleAndWater";
import DayOrHourTimePicker from "../../../../../../compontents/DayOrHourTimePicker";

import "./index.less";

export interface CodeConditionProps {
  onChange: (paramJson: CodeConditionParam) => any;
  query: (paramJson: CodeConditionParam) => any;
}

export interface CodeConditionState {}

export interface CodeConditionParam {
  energyType: number;
  likeStr: string;
  timeType: number;
  startTime: number;
  endTime: number;
}

class CodeCondition extends React.Component<
  CodeConditionProps,
  CodeConditionState
> {
  public paramJson: CodeConditionParam = {
    energyType: 1,
    likeStr: "",
    timeType: 2,
    startTime: 0,
    endTime: 0,
  };
  render() {
    return (
      <div className="Condition">
        <div className="rowcon_body" style={{ paddingTop: 16 }}>
          <div className="paramRow">
            <EleAndWater
              onChange={(type: number) => {
                this.paramJson.energyType = type;
                this.props.onChange(this.paramJson);
              }}
            />
            <div className="device">
              设备：
              <Input
                placeholder="设备名称或地址"
                onChange={(event) => {
                  this.paramJson.likeStr = event.target.value;
                  this.props.onChange(this.paramJson);
                }}
              />
            </div>
            <DayOrHourTimePicker
              defaultvalue={[moment().add(-3, "day"), moment()]}
              onChange={(timeType: number, dateString: string[]) => {
                this.getDate(timeType, dateString);
                this.props.onChange(this.paramJson);
              }}
            />
            <Button
              className="btn"
              type="primary"
              onClick={() => {
                this.onSubmit();
              }}
            >
              查询
            </Button>
          </div>
        </div>
      </div>
    );
  }
  componentDidMount() {
    setTimeout(() => {
      this.onSubmit();
    }, 100);
  }
  onSubmit() {
    this.props.query(this.paramJson);
  }
  getDate(timeType: number, dateString: string[]) {
    this.paramJson.timeType = timeType;
    if (dateString && dateString[0] && dateString[1]) {
      this.paramJson.startTime = moment(
        dateString[0],
        "YYYY/MM/DD HH"
      ).valueOf();
      this.paramJson.endTime = moment(dateString[1], "YYYY/MM/DD HH").valueOf();
    }
  }
}
export default CodeCondition;
